﻿using System;
using System.Data;
using VitCode.Web.UI.WebControls;

namespace CRS.Web.Selected
{
    public partial class SelGoodsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pgList.DataSourceDelegate=new VitCode.Web.UI.PaginationGrid.GridDataSourceDelegate(BindData);
            if(!IsPostBack)
            {
                Session["dataSources"] = null;
                BindDrpList();
                pgList.BindGrid();
            }
        }

        private void BindDrpList()
        {
            drpDownList.Items.Add("货品条码");
            drpDownList.Items.Add("货品名称");
            drpDownList.Items.Add("规格");
            drpDownList.Items.Add("类别");
            drpDownList.Items.Add("进价");
            drpDownList.Items.Add("售价");
            drpDownList.Items.Add("最低库存");
            drpDownList.Items.Add("当前库存");
        }

        private DataTable _dataSources;
        private DataTable DataSource
        {
            get
            {
                if(_dataSources==null)
                {
                    _dataSources = InitData();
                    Session["dataSources"] = _dataSources;
                }
                else
                {
                    if (Session["dataSources"] != null)
                        _dataSources = (DataTable)Session["dataSources"];
                    else
                    {
                        Session["dataSources"] = _dataSources;
                    }
                }
                return _dataSources;
            }
        }

        private object BindData()
        {
            return DataSource;
        }

        private DataTable InitData()
        {   
            var table = new DataTable();
            table.Columns.Add("货品条码");
            table.Columns.Add("货品名称");
            table.Columns.Add("规格");
            table.Columns.Add("类别");
            table.Columns.Add("进价");
            table.Columns.Add("售价");
            table.Columns.Add("最低库存");
            table.Columns.Add("当前库存");

            for (int i = 0; i < 100; i++)
            {
                var row = table.NewRow();
                row["货品条码"] = "VICCode_" + i.ToString().PadLeft(10, '0');
                row["货品名称"] = "啤酒" + i.ToString();
                row["规格"] = "1*600";
                row["类别"] = "酒类" + i.ToString();
                row["进价"] = i.ToString();
                row["售价"] = (i + 5).ToString();
                row["最低库存"] = "24支";
                row["当前库存"] = "240支";                // 条码、品名、规格、数量、单位
                table.Rows.Add(row);
            }
            return table;
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Window.Close(new string[]{"",""});
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            var selects = pgList.GetSelectedItem(1);
            var rows = selects.Split(new char[] {','});
            var selectValue = new string[rows.Length];
            for (var i = 0; i < rows.Length; i++)
            {
                var row = DataSource.Select("货品条码='" + rows[i] + "'");
                if (row.Length > 0)
                {
                    var s = row[0]["货品条码"].ToString() + "," + row[0]["货品名称"].ToString() + "," + row[0]["规格"].ToString() +
                            "," + "20" + "," + "支";
                    selectValue[i] = s;
                }
            }
            Session["m_SelectedGoods"] = selectValue;
            Window.Close();
        }
    }
}
